package zhengyanze.com.aipoemmax.guide.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import zhengyanze.com.aipoemmax.R;
import zhengyanze.com.aipoemmax.guide.adapter.MainAdapter;
import zhengyanze.com.aipoemmax.guide.bean.PoemBean;
import zhengyanze.com.aipoemmax.guide.bean.PoemBean2;
import zhengyanze.com.aipoemmax.guide.utils.LocationUtils;

import static zhengyanze.com.aipoemmax.guide.activity.PoemDetilsActivity.POEM_KEY;

public class LocationCollectActivity extends AppCompatActivity {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.rv_local)
    RecyclerView rvLocal;
    @BindView(R.id.has_data)
    TextView hasData;

    ArrayList<PoemBean> mPoemBeans = new ArrayList<>();
    private MainAdapter mainAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_location_collect);
        ButterKnife.bind(this);
        initView();
        getData();
    }

    public void initView() {
        setSupportActionBar(toolbar);
        setTitle("本地收藏");
        rvLocal.setLayoutManager(new LinearLayoutManager(this));
        mainAdapter = new MainAdapter();
        rvLocal.setAdapter(mainAdapter);
    }

    /**
     * 获取本地文件中的数据
     */
    public void getData() {
        LocationUtils utils = LocationUtils.getLocation(LocationCollectActivity.this);
        ArrayList<PoemBean2> poemBeens = utils.getFile();
        if (poemBeens != null) {
            for (PoemBean2 bean2 : poemBeens) {
                mPoemBeans.add(new PoemBean(bean2.getTitle(), bean2.getAuthor(), bean2.getContent(), bean2.getLink()));
            }
            mainAdapter.notifyDataSetChanged();
        }
    }

    /**
     * 适配器
     */
    class MainAdapter extends RecyclerView.Adapter<MainAdapter.MainViewHolder> {

        public class MainViewHolder extends RecyclerView.ViewHolder {
            TextView tv_poemt;
            TextView tv_poema;
            TextView tv_poemc;
            TextView delete;

            public MainViewHolder(View itemView) {
                super(itemView);
                tv_poemt = itemView.findViewById(R.id.tv_poemt);
                tv_poema = itemView.findViewById(R.id.tv_poema);
                tv_poemc = itemView.findViewById(R.id.tv_poemc);
                delete = itemView.findViewById(R.id.delete);
            }
        }

        @Override
        public MainAdapter.MainViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_main, parent, false);
            return new MainAdapter.MainViewHolder(view);
        }

        boolean isEdit = false;
        @Override
        public void onBindViewHolder(final MainAdapter.MainViewHolder holder, final int position) {
            final PoemBean bean = mPoemBeans.get(position);
            holder.tv_poemt.setText(bean.getTitle());
            holder.tv_poema.setText(bean.getAuthor());
            holder.tv_poemc.setText(bean.getContent().split("。")[0]);



            //如果处在编辑模式
            if (isEdit) {
                holder.delete.setVisibility(View.VISIBLE);
                holder.delete.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        mPoemBeans.remove(position);
                        notifyDataSetChanged();
                        if (mPoemBeans.isEmpty()) {
                            saveData();
                        }
                    }
                });
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        isEdit = false;
                        notifyDataSetChanged();
                        saveData();
                    }
                });
            }else{
                holder.delete.setVisibility(View.GONE);
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(holder.itemView.getContext(), PoemDetilsActivity.class);
                        intent.putExtra(POEM_KEY, bean);
                        holder.itemView.getContext().startActivity(intent);
                    }
                });
            }

            //长按进入编辑模式
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    isEdit = true;
                    notifyDataSetChanged();
                    return false;
                }
            });
        }

        @Override
        public int getItemCount() {
            if (mPoemBeans.isEmpty()) {
                hasData.setVisibility(View.VISIBLE);
            }else{
                hasData.setVisibility(View.GONE);
            }
            return mPoemBeans.size();
        }

        public void saveData() {
            ArrayList<PoemBean2> poenBean2s = new ArrayList<PoemBean2>();
            for (PoemBean bean : mPoemBeans) {
                poenBean2s.add(new PoemBean2(bean.getTitle(), bean.getAuthor(), bean.getContent(), bean.getLink()));
            }
            LocationUtils utils = LocationUtils.getLocation(LocationCollectActivity.this);
            utils.saveFile(poenBean2s);
        }
    }
}
